const miniMist = require('minimist')
const { resolve } = require('path')

const { build } = require('esbuild')

let args = process.argv.slice(2)

args = miniMist(args)
// 要对哪个子项目进行打包
const target = args._[0] || 'reactivity'

// 指定打包的格式
const f = args.f || 'global'

build({
    entryPoints: [resolve(__dirname, `../packages/${target}/src/index.ts`)],
    outfile: resolve(__dirname, `../packages/${target}/dist/${target}.${f}.js`),
    bundle: true, // 把所有的文件打包成一个文件
    sourcemap: true, // 调试映射
    format: f === 'global' ? 'iife' : f === 'cjs' ? 'cjs': 'esm' , // 打包格式
    globalName: require(resolve(__dirname, `../packages/${target}/package.json`)).buildOptions.name, 
    platform: f === 'cjs' ? 'node' : 'browser', // 打包平台
    // 监听打包
    watch: {
        onRebuild(error) {
            if (!error) console.log('构建成功')
        }
    }
}).then(() => console.log('初始化构建成功'))